package minh.bac.wcbrazil2014.netsupport;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.json.JSONException;

import minh.bac.wcbrazil2014.database.MySQLiteHelper;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import minh.bac.wcbrazil2014.object.XepHangObj;
import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;

public class WCAsynctask extends AsyncTask<String, Void, Boolean> {

	ControlAsynctask controlAsynctask;
	Context context;

	public WCAsynctask(Context context, ControlAsynctask controlAsynctask) {
		this.context = context;
		this.controlAsynctask = controlAsynctask;
	}

	@Override
	protected Boolean doInBackground(String... params) {
		// TODO Auto-generated method stub
		String res = NetSupport.getInstance().method_GET(params[0]);
		if (res == null) {
			return false;
		} else {
			ArrayList<FootBallTeamObj> arrayListFB = new ArrayList<FootBallTeamObj>();
			LinkedHashMap<String, ArrayList<MatchScheduleObj>> hashMap = new LinkedHashMap<>();
			ArrayList<XepHangObj> arrayListXH = new ArrayList<XepHangObj>();
			try {

				arrayListFB = JsonParser.getThongTinDoiBong(res);
				hashMap = JsonParser.getLichDauVongBang(res);
				arrayListXH = JsonParser.getXepHangBang(res);
				if ((arrayListFB == null) || (hashMap == null)) {
					return false;
				}
				//

				// ---------------------------------- save to
				// database----------------------------------------------------------
				String[] vongDau = { "vongbang", "vong1/8", "vongtuket",
						"vongbanket", "giai3,4", "chungket" };
				// //String[] xephang = { "bangxephang" };
				String TableName;
				ContentValues contentValues;
				//
				// save to table footballteam detail
				//
				MySQLiteManager mySQLiteManager = new MySQLiteManager(context);
				mySQLiteManager
						.deleteRowTable(MySQLiteHelper.NAME_TABLE_VONBANG);
				mySQLiteManager
						.deleteRowTable(MySQLiteHelper.NAME_TABLE_VONGDAU);
				mySQLiteManager
						.deleteRowTable(MySQLiteHelper.NAME_TABLE_XEPHANG);
				//
				TableName = MySQLiteHelper.NAME_TABLE_DOIBONG;
				for (FootBallTeamObj item : arrayListFB) {

					contentValues = new ContentValues();
					//
					contentValues
							.put(MySQLiteHelper.doiBong, item.getDoiBong());
					contentValues
							.put(MySQLiteHelper.bangDau, item.getBangDau());
					contentValues.put(MySQLiteHelper.thuMon, item.getThuMon());
					contentValues.put(MySQLiteHelper.hauVe, item.getHauVe());
					contentValues.put(MySQLiteHelper.tienVe, item.getTienVe());
					contentValues
							.put(MySQLiteHelper.tienDao, item.getTienDao());
					contentValues.put(MySQLiteHelper.tuCachThamDu,
							item.getTuCachThamDu());
					contentValues.put(MySQLiteHelper.xepHangFiFa,
							item.getXepHangFiFa());
					contentValues.put(MySQLiteHelper.lichSuThamDu,
							item.getLichSuThamDu());
					contentValues.put(MySQLiteHelper.favorite,
							item.getFavorite());
					//
					boolean checkInsert = mySQLiteManager.insert(TableName,
							contentValues);
					if (checkInsert == false) {
						continue;
					}
				}

				TableName = MySQLiteHelper.NAME_TABLE_VONBANG;
				ArrayList<MatchScheduleObj> vongbangList = new ArrayList<MatchScheduleObj>();
				vongbangList = hashMap.get(vongDau[0]);
				for (int i = 0; i < vongbangList.size(); i++) {
					// public static String tran = "tran", ngay = "ngay", gio =
					// "gio", doi1 = "doi1", imgLinkDoi1 = "imglinkdoi1",
					// score1 = "score1", doi2 = "score2", imgLinkdoi2 =
					// "imglinkdoi2", score2 = "score2", diaDiem = "diadiem",
					// kenhTV = "kenhtv";
					VongBangObj item = (VongBangObj) vongbangList.get(i);
					contentValues = new ContentValues();
					//
					contentValues
							.put(MySQLiteHelper.bangDau, item.getBangDau());
					contentValues.put(MySQLiteHelper.tran, item.getTran());
					contentValues.put(MySQLiteHelper.ngay, item.getNgay());
					contentValues.put(MySQLiteHelper.gio, item.getGio());
					contentValues.put(MySQLiteHelper.doi1, item.getDoi1());
					contentValues.put(MySQLiteHelper.imgLinkDoi1,
							item.getImgLinkDoi1());
					contentValues.put(MySQLiteHelper.score1, item.getScore1());
					contentValues.put(MySQLiteHelper.doi2, item.getDoi2());
					contentValues.put(MySQLiteHelper.imgLinkdoi2,
							item.getImgLinkdoi2());
					contentValues.put(MySQLiteHelper.score2, item.getScore2());
					contentValues
							.put(MySQLiteHelper.diaDiem, item.getDiaDiem());
					contentValues.put(MySQLiteHelper.kenhTV, item.getKenhTV());
					//
					boolean checkInsert = mySQLiteManager.insert(TableName,
							contentValues);
					if (checkInsert == false) {
						continue;
					}
				}
				//
				TableName = MySQLiteHelper.NAME_TABLE_VONGDAU;
				for (int i = 1; i < vongDau.length; i++) {
					ArrayList<MatchScheduleObj> vongdauList = new ArrayList<MatchScheduleObj>();
					vongdauList = hashMap.get(vongDau[i]);
					for (int j = 0; j < vongdauList.size(); j++) {
						// public static String tran = "tran", ngay = "ngay",
						// gio = "gio", doi1 = "doi1", imgLinkDoi1 =
						// "imglinkdoi1",
						// score1 = "score1", doi2 = "score2", imgLinkdoi2 =
						// "imglinkdoi2", score2 = "score2", diaDiem =
						// "diadiem", kenhTV = "kenhtv";
						VongDau item = (VongDau) vongdauList.get(j);
						contentValues = new ContentValues();
						//
						contentValues.put(MySQLiteHelper.vong, item.getVong());
						contentValues.put(MySQLiteHelper.tran, item.getTran());
						contentValues.put(MySQLiteHelper.ngay, item.getNgay());
						contentValues.put(MySQLiteHelper.gio, item.getGio());
						contentValues.put(MySQLiteHelper.doi1, item.getDoi1());
						contentValues.put(MySQLiteHelper.imgLinkDoi1,
								item.getImgLinkDoi1());
						contentValues.put(MySQLiteHelper.score1,
								item.getScore1());
						contentValues.put(MySQLiteHelper.doi2, item.getDoi2());
						contentValues.put(MySQLiteHelper.imgLinkdoi2,
								item.getImgLinkdoi2());
						contentValues.put(MySQLiteHelper.score2,
								item.getScore2());
						contentValues.put(MySQLiteHelper.diaDiem,
								item.getDiaDiem());
						contentValues.put(MySQLiteHelper.kenhTV,
								item.getKenhTV());
						//
						boolean checkInsert = mySQLiteManager.insert(TableName,
								contentValues);
						if (checkInsert == false) {
							continue;
						}
					}
				}

				TableName = MySQLiteHelper.NAME_TABLE_XEPHANG;
				// ArrayList<XepHangObj> xephangList = new
				// ArrayList<XepHangObj>();
				for (int i = 0; i < arrayListXH.size(); i++) {
					XepHangObj item = (XepHangObj) arrayListXH.get(i);
					contentValues = new ContentValues();
					//
					contentValues
							.put(MySQLiteHelper.bangDau, item.getBangDau());
					contentValues
							.put(MySQLiteHelper.doiBong, item.getDoiBong());
					contentValues.put(MySQLiteHelper.image, item.getImage());
					contentValues.put(MySQLiteHelper.tran, item.getTran());
					contentValues.put(MySQLiteHelper.thb, item.getThb());
					contentValues.put(MySQLiteHelper.hieuso, item.getHieuSo());
					contentValues.put(MySQLiteHelper.diem, item.getDiem());

					boolean checkInsert = mySQLiteManager.insert(TableName,
							contentValues);
					if (checkInsert == false) {
						continue;
					}
				}
				//
				return true;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return false;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		controlAsynctask.doInCompleted(result);
	}

}
